using System;
using UnityEngine;

public class SingletonMonoBehaviour<T> : MonoBehaviour where T : SingletonMonoBehaviour<T>
{
	public static T Instance
	{
		get
		{
			if (null == SingletonMonoBehaviour<T>.instance)
			{
				SingletonMonoBehaviour<T>.instance = (UnityEngine.Object.FindObjectOfType(typeof(T)) as T);
				if (SingletonMonoBehaviour<T>.instance == null)
				{
					UnityEngine.Debug.LogWarning("No instance of " + typeof(T).ToString() + ", a temporary one is created.");
					SingletonMonoBehaviour<T>.instance = new GameObject("Temp Instance of " + typeof(T).ToString(), new Type[]
					{
						typeof(T)
					}).GetComponent<T>();
					if (SingletonMonoBehaviour<T>.instance == null)
					{
						UnityEngine.Debug.LogError("Problem during the creation of " + typeof(T).ToString());
					}
				}
			}
			return SingletonMonoBehaviour<T>.instance;
		}
	}

	protected virtual void Awake()
	{
		if (null != SingletonMonoBehaviour<T>.instance && this as T != SingletonMonoBehaviour<T>.instance)
		{
			UnityEngine.Debug.LogError("Destory for SingletonMonoBehaviour");
			UnityEngine.Object.Destroy(this);
		}
		else
		{
			SingletonMonoBehaviour<T>.instance = (this as T);
			SingletonMonoBehaviour<T>.instance.Init();
		}
	}

	public virtual void Init()
	{
	}

	private void OnApplicationQuit()
	{
		SingletonMonoBehaviour<T>.instance = (T)((object)null);
	}

	public void Idle()
	{
	}

	public bool IsValid()
	{
		return null != SingletonMonoBehaviour<T>.Instance && this as T == SingletonMonoBehaviour<T>.instance;
	}

	private static T instance;
}
